#include "Carousel.h"
#include <windows.h>
#include <GL/gl.h>

CCarousel::CCarousel(float size): m_size(size), m_rotationAngle(this, 0), m_upAndDown1(this, 0), m_upAndDown2(this, 0), 
m_upAndDown3(this, 0), m_upAndDown4(this, 0), m_upAndDown5(this, 0), m_upAndDown6(this, 0), m_upAndDown7(this, 0), m_upAndDown8(this, 0)
{}

CCarousel::~CCarousel(void)
{
}

void CCarousel::doRender()
{
	glPushMatrix();
	//rotate everything
	glRotatef(m_rotationAngle, 0, 1, 0);
		//draw the ground of the carousel
		drawBase(m_size*20);
		
		//draw the stufs that go up and down
		int offsetUpAndDown=6+m_size;
		glPushMatrix();
			glTranslatef(m_size*5, m_upAndDown1+offsetUpAndDown, m_size*5);
			drawCube(m_size*2);
		glPopMatrix();
		glPushMatrix();
			glTranslatef(m_size*5, m_upAndDown2+offsetUpAndDown, 0);
			drawCube(m_size*2);
		glPopMatrix();
		glPushMatrix();
			glTranslatef(m_size*5, m_upAndDown3+offsetUpAndDown, -m_size*5);
			drawCube(m_size*2);
		glPopMatrix();
		glPushMatrix();
			glTranslatef(0, m_upAndDown4+offsetUpAndDown, -m_size*5);
			drawCube(m_size*2);
		glPopMatrix();
		glPushMatrix();
			glTranslatef(-m_size*5, m_upAndDown5+offsetUpAndDown, -m_size*5);
			drawCube(m_size*2);
		glPopMatrix();
		glPushMatrix();
			glTranslatef(-m_size*5, m_upAndDown6+offsetUpAndDown, 0);
			drawCube(m_size*2);
		glPopMatrix();
		glPushMatrix();
			glTranslatef(-m_size*5, m_upAndDown7+offsetUpAndDown, m_size*5);
			drawCube(m_size*2);
		glPopMatrix();
		glPushMatrix();
			glTranslatef(0, m_upAndDown8+offsetUpAndDown, m_size*5);
			drawCube(m_size*2);
		glPopMatrix();

		//draw the pillar in the middle of the carousel
		glTranslatef(0, 0.5, 0);
		drawCube(m_size*2);
		glTranslatef(0, m_size*2, 0);
		drawCube(m_size*2);
		glTranslatef(0, m_size*2, 0);
		drawCube(m_size*2);
		glTranslatef(0, m_size*2, 0);
		drawCube(m_size*2);
		glTranslatef(0, m_size*2, 0);
		drawCube(m_size*2);
		glTranslatef(0, m_size*2, 0);
		drawCube(m_size*2);

		//draw the top of the carousel
		drawBase(m_size*20);

	glPopMatrix();
}

void CCarousel::drawBase(float size)
{
	glPushMatrix();
		glBegin(GL_QUADS);
		glNormal3f(0, -1, 0);
			float halfSize=size/2;
			float height=0.5;
			glVertex3f(halfSize, 0, halfSize);
			glVertex3f(halfSize, 0, -halfSize);
			glVertex3f(-halfSize, 0, -halfSize);
			glVertex3f(-halfSize, 0, halfSize);

			glNormal3f(1, 0, 0);
			glVertex3f(halfSize, 0, halfSize);
			glVertex3f(halfSize, 0, -halfSize);
			glVertex3f(halfSize, height, -halfSize);
			glVertex3f(halfSize, height, halfSize);

			glNormal3f(0, 0, 1);
			glVertex3f(halfSize, 0, halfSize);
			glVertex3f(-halfSize, 0, halfSize);
			glVertex3f(-halfSize, height, halfSize);
			glVertex3f(halfSize, height, halfSize);

			glNormal3f(-1, 0, 0);
			glVertex3f(-halfSize, 0, -halfSize);
			glVertex3f(-halfSize, 0, halfSize);
			glVertex3f(-halfSize, height, halfSize);
			glVertex3f(-halfSize, height, -halfSize);

			glNormal3f(0, 0, -1);
			glVertex3f(halfSize, 0, -halfSize);
			glVertex3f(halfSize, height, -halfSize);
			glVertex3f(-halfSize, height, -halfSize);
			glVertex3f(-halfSize, 0, -halfSize);

			glNormal3f(0, 1, 0);
			glVertex3f(halfSize, height, halfSize);
			glVertex3f(halfSize, height, -halfSize);
			glVertex3f(-halfSize, height, -halfSize);
			glVertex3f(-halfSize, height, halfSize);
		glEnd();
	glPopMatrix();
}

void CCarousel::drawCube(float size)
{
	glPushMatrix();
	glTranslatef(-size/2, -size/2, -size/2);
	glBegin(GL_QUADS);
		
		glNormal3f(0, 0, -1);
		glVertex3f(0, 0, 0);
		glVertex3f(size, 0, 0);
		glVertex3f(size, size, 0);
		glVertex3f(0, size, 0);
		
		//ok
		glNormal3f(1, 0, 0);
		glVertex3f(size, size, 0);
		glVertex3f(size, 0, 0);
		glVertex3f(size, 0, size);
		glVertex3f(size, size, size);
		
		//ok
		glNormal3f(-1, 0, 0);
		glVertex3f(0, 0, 0);
		glVertex3f(0, size, 0);
		glVertex3f(0, size, size);
		glVertex3f(0, 0, size);
		
		glNormal3f(0, 0, 1);
		glVertex3f(0, size, size);
		glVertex3f(0, 0, size);
		glVertex3f(size, 0, size);
		glVertex3f(size, size, size);
		
		//ok
		glNormal3f(0, 1, 0);
		glVertex3f(0, size, size);
		glVertex3f(size, size, size);
		glVertex3f(size, size, 0);
		glVertex3f(0, size, 0);

		//ok
		glNormal3f(0, -1, 0);
		glVertex3f(0, 0, 0);
		glVertex3f(size, 0, 0);
		glVertex3f(size, 0, size);
		glVertex3f(0, 0, size);
			
	glEnd();
	glPopMatrix();
}
